package com.genericdevice.platform.pages.main.activity;

import android.os.Bundle;
import com.genericdevice.platform.R;
import com.genericdevice.platform.core.activity.AbstractActivity;
import com.genericdevice.platform.core.data.DataBase;
import com.genericdevice.platform.core.define.EventConstants;
import com.genericdevice.platform.core.event.EventBus;
import com.genericdevice.platform.core.event.IEvent;
import com.genericdevice.platform.core.event.listeners.PluginChangeListener;
import com.genericdevice.platform.core.mvp.AbstractPresenter;
import com.genericdevice.platform.core.plugins.IPlugin;
import com.genericdevice.platform.core.plugins.PluginsLoader;
import com.genericdevice.platform.pages.main.mvp.MainPresenter;


public class Main extends AbstractActivity<AbstractPresenter> {

    private IPlugin plugin;

    public Main() {
        DataBase.initializeDB(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.addListener(EventConstants.PLUGIN_CHANGED, new PluginChangeListener(false) {
            @Override
            protected void onChange(IEvent event) {
                init();
            }
        });
    }

    @Override
    protected void init() {
        plugin = PluginsLoader.loadSelectedPlugin();

        if (plugin != null)
            plugin.init();

        super.init();
    }

    @Override
    protected AbstractPresenter createPresenter() {
        if (plugin != null)
            return plugin.getPresenter();
        else
            return new MainPresenter();
    }

    @Override
    protected int getMenuId() {
        return R.menu.main;
    }

    @Override
    protected Integer getResourceId() {
        return R.xml.main_options_menu;
    }

    @Override
    protected Class getIdsObject() {
        return R.id.class;
    }
}
